var util = require('../../md5.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:'',
    code:'',
    phone:'',
    inviteCode:'',
    createpath: ''
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    const ctx = wx.createCanvasContext('mycanas')
    ctx.setFillStyle("#f3f4c8")
    ctx.setFontSize(20)
    ctx.fillText('20', 20, 20)
    ctx.setFontSize(30)
    ctx.fillText('30', 40, 40)
    ctx.setFontSize(40)
    ctx.fillText('40', 60, 60)
    ctx.setFontSize(50)
    ctx.fillText('50', 90, 90)

    ctx.draw()
    setTimeout(function () {
      wx.canvasToTempFilePath({

        width: 750,
        height: 1279,
        destWidth: 750,
        destHeight: 1279,
        canvasId: 'mycanas',
        quality: 1,
        success: function (res) {

          that.setData({
            createpath: res.tempFilePath
          })
          that.cc()
        }, fail(e) {
          console.log(e)
        }
      })
    }, 1000)
  },
  cc: function () {
    var that = this
    setTimeout(function () {
      wx.saveImageToPhotosAlbum({
        filePath: that.data.createpath,
        success: (res) => {
          wx.hideLoading()
          console.log(res)
          that.setData({
            canvasHidden: true
          })
        },
        fail: (err) => {
          console.log(err)
        }
      })
    }, 1000)
  },
  send:function(){
    wx.showLoading({
      title: '请稍候',
    })
    var that = this
    wx.login({
      success: function (res) {
        var code = res.code;
        that.getPayInfo(code)
        console.log("code")
        console.log(code)
      }, fail: function () {
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: '请再次请求',
        showCancel: false
      })
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '请再次请求',
          showCancel: false
        })
        wx.hideLoading()
      }

    })
  },

  sendPayMent: function (nonce_str, prepay_id){
  var that = this
    var time = Date.parse(new Date())/1000; 
    var paySign = util.hexMD5('appId=wxd59c11f9decb5e58&nonceStr='+nonce_str+'&package=prepay_id='+prepay_id+'&signType=MD5&timeStamp='+time+'&key=lkjmkluhjjm3256jkk52188iojkklok5') 

  wx.requestPayment({
    'timeStamp': time+'',
    'nonceStr': nonce_str,
    'package':'prepay_id='+prepay_id,
    'signType': 'MD5',
    'paySign': paySign,
    'success': function (res) {
      console.log('8888')
      console.log(res)
      // wx.redirectTo({
      //   url: '../buySuccess/buySuccess?num=' + that.data.num + '&phone=' + that.data.phone,
      //     })
    },
    'fail': function (res) {
      console.log('fail')
      console.log(res)
    }
  })
},
  
  getPayInfo:function(codee){
    console.log(codee)
    var that = this
    wx.request({
      url: 'https://xiaochengxu.fincoder.com/index.php/Home/WeiPayTest/prePayProcess',
      data: {
        oid: that.data.num,
        code: codee,
      
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
    
        console.log(res)

        if (res.data.status === 1) {
          that.sendPayMent(res.data.nonce_str, res.data.prepay_id)
          console.log(res.data.prepay_id)
          console.log("898989")
        }
        else {
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '请求失败',
            showCancel: false
          })
        }
      }, fail: function (e) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请求失败',
          showCancel: false
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  toSuccess: function () {

    // wx.navigateTo({ url: '../buySuccess/buySuccess' })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})